package com.example.employeemanagement.mapper;

import com.example.employeemanagement.entry.Department;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 部门数据访问接口
 * 使用MyBatis注解方式实现部门的CRUD操作
 */
@Mapper
public interface DepartmentMapper {
    /**
     * 查询所有部门
     * @return 部门列表
     */
    @Select("SELECT * FROM department")
    List<Department> getAllDepartments();

    /**
     * 根据ID查询部门
     * @param departmentId 部门ID
     * @return 部门信息
     */
    @Select("SELECT * FROM department WHERE department_id = #{departmentId}")
    Department getDepartmentById(@Param("departmentId") Integer departmentId);

    /**
     * 检查部门ID是否存在
     * @param departmentId 部门ID
     * @return 是否存在
     */
    @Select("SELECT COUNT(*) > 0 FROM department WHERE department_id = #{departmentId}")
    boolean existsById(@Param("departmentId") Integer departmentId);

    /**
     * 添加部门
     * @param department 部门信息
     * @return 影响行数
     */
    @Insert("INSERT INTO department (department_name, description) VALUES (#{departmentName}, #{description})")
    @Options(useGeneratedKeys = true, keyProperty = "departmentId")
    int addDepartment(Department department);

    /**
     * 更新部门
     * @param department 部门信息
     * @return 影响行数
     */
    @Update("UPDATE department SET department_name = #{departmentName}, description = #{description} WHERE department_id = #{departmentId}")
    int updateDepartment(Department department);

    /**
     * 删除部门
     * @param departmentId 部门ID
     * @return 影响行数
     */
    @Delete("DELETE FROM department WHERE department_id = #{departmentId}")
    int deleteDepartment(@Param("departmentId") Integer departmentId);
}